# 爬取爱奇艺风云电影榜数据并保存到Excel文件
import requests
import pandas as pd
import urllib3
import os

# 禁用InsecureRequestWarning警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 初始化DataFrame
df = pd.DataFrame(columns=["id", '剧名', '简介', '链接', '热度', '弹幕', '标签'])
num = 1

# 设置请求头
header = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
}

# 循环发送请求并处理数据
for page in range(1, 5):
    url = 'https://pcw-api.iqiyi.com/strategy/pcw/data/topRanksData?page_st=0&tag=0&category_id=1&date=&pg_num={0}'.format(page)
    resp = requests.get(url, headers=header, verify=False)
    resp.encoding = 'utf-8'
    data = resp.json().get('data', {}).get('formatData', {}).get('data', {}).get('content', [])

    for i in data:
        df.at[num, "id"] = i.get('tvid')
        df.at[num, "剧名"] = i.get('title')
        df.at[num, "简介"] = i.get('desc')
        df.at[num, "链接"] = i.get('pageUrl')
        df.at[num, "热度"] = i.get('index')
        df.at[num, "弹幕"] = i.get('danmu')
        df.at[num, "标签"] = i.get('tags')

        num += 1

    resp.close()

# 确保目标目录存在
file_path = '../data/20250527爱奇艺风云榜电影榜.xlsx'
directory = os.path.dirname(file_path)

if not os.path.exists(directory):
    os.makedirs(directory)

# 将DataFrame写入Excel文件
df.to_excel(file_path, index=False)

print(f"数据已成功写入 {file_path}")
